<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Service;
use Admin\Controller\OssController;
use Admin\Enums\AuthorType;
use Admin\Enums\CreateType;
use Admin\Enums\SheetStatus;

/**
 * 曲谱服务
 * @author Administrator
 *
 */
class SheetService{
	
    /**
     * 新增曲谱
     */
    public function add(){
        $param = I('post.');
        //step-1: 验证参数
        $Model = D('Sheet');
        $data = $Model->create($param);
        if($data){
            //step-2: 设置默认参数
            $param = $this->fillParam($param);
            
            //step-3: 保存谱单
            $id = $Model->add($param);


            //step-4:将本地图片上传阿里云
            $ossController = new OssController();
            $imageIds = $ossController->uploadSheet($param);

            //step-5: 保存曲谱和图片的关系
            $imageSheetService = new ImageSheetService();
            $imageSheetService->save($imageIds, $id);
            
            //step-6: 保存标签
            $tags = explode("," ,$param['tags']);
            $tagService = new TagService();
            foreach ($tags as $key=>$name) {
                $tag['name'] = $name;
                $tag_id = $tagService->save($tag);
                $tag['id']  = $tag_id;
                $tags[$key] = $tag;
            }
            
            //step-7: 保存标签和曲谱的关系
            $tag_ids = array_column($tags, "id");
            $tagSheetService = new TagSheetService();
            $tagSheetService->save($tag_ids, $id);
            
            return $id;
        } else {
            E($Model->getError());
        }
        
	}
	
	/**
	 * 编辑曲谱
	 */
	public function edit(){
	    $param = I('post.');
	
	    //step-1: 验证参数
	    $Model = D('Sheet');
	    $data = $Model->create($param);
	    if($data){
	        //step-2: 设置默认参数
	        $param['updator']  = 2;
	        $param['update_time']  = time();
	
	        //step-3: 保存谱单
	        $id = $Model->save($param);

            //step-4:将本地图片上传阿里云
            $ossController = new OssController();
            $imageIds = $ossController->uploadSheet($param);
	
	        //step-5: 保存曲谱和图片的关系
	        $imageSheetService = new ImageSheetService();
	        $imageSheetService->save($imageIds, $param['id']);
	
	        //step-6: 保存标签
	        $tags = explode("," ,$param['tags']);
            $tagService = new TagService();
            foreach ($tags as $key=>$name) {
                $tag['name'] = $name;
                $tag_id = $tagService->save($tag);
                
                $tag['id']  = $tag_id;
                $tags[$key] = $tag;
                
                unset($tag);
            }
            
            //step-6: 保存标签和曲谱的关系
            $tag_ids = array_column($tags, "id");
            
            $tagSheetService = new TagSheetService();
            $tagSheetService->save($tag_ids, $param['id']);
	
	        return $id;
	    } else {
	        E($Model->getError());
	    }
	
	}
	
	/**
	 * 填充参数
	 * @param unknown $param
	 */
	private function fillParam($param){
	    $param['author_type'] = CreateType::$ADMIN;
	    $param['creator'] = UID;
	    $param['status'] = SheetStatus::$UNCHECKED;
	    
	    return $param;
	}
	
	public function convertList($list, $sheetScoreMap = array()){
	    //step-1: 查询创建人信息
	    $creatorIdList = array_column($list,'creator');
	    $memberService = new MemberService();
	    $creatorMap = $memberService->getMapByUids($creatorIdList);

	    //step-2: 自动填充标签
	    $sheetIdList = array_column($list,'id');
	    $tagSheetService = new TagSheetService();
	    $tagsMap = $tagSheetService->getTagsBySheetIds($sheetIdList);
	    
	    foreach ($list as $key=>$sheet) {
	        $member = $creatorMap[$sheet['creator']];
	        $list[$key]['creator_name'] = empty($member) ? "" : $member['nickname'];
	        $list[$key]['status_name'] = SheetStatus::$map[$sheet['status']];
	        $list[$key]['tags'] = $tagsMap[$sheet['id']];
	        if(!empty($sheetScoreMap)) $list[$key]['id'] = $sheetScoreMap[$sheet['id']];
	    }
	    return $list;
	}
	
	/**
	 * 提交（审核通过）
	 */
	public function submit($map){
	    $param['status'] = SheetStatus::$SUCCESS;
	    $param['checker'] = UID; 
	    $param['check_time'] = date('Y-m-d H:i:s',time());
	    return M('Sheet')->where($map)->save($param);
	}
	
	public function listByIds($scoreSheetList){
	    $sheetIdList = array_column($scoreSheetList, "sheet_id");
	    $map['id'] = array('in', $sheetIdList);
	    $sheetList = M('Sheet')->where($map)->select();

	    return $this->fillInfo($sheetList);
	    
	}

    /**
     * 获取曲谱的基本信息
     * @param $scoreIds
     * @return array
     */
    public function getSheetInfoByIds($sheetIds)
    {
        $map = array('id' => array('in', $sheetIds));

        $Model = M('Sheet');
        $sheetList = $Model->where($map)->select();
        $sheetMap = array();
        foreach ($sheetList as $key=> $val) {
            $sheetMap[$val['id']] = $val;
        }
        return $sheetMap;
    }

    /**
     * 补充用户信息
     */
    public function fillInfo($list, $order = 'id', $sort = 1)
    {

        //step-1: 根据创建者类型分为两组
        foreach ($list as $key => $val) {
            if ($val['author_type'] == AuthorType::$ADMIN) {
                $admin[] = $val;
            } else {
                $user[] = $val;
            }
        }

        if (!empty($user)) {
            $creatorIdList = array_column($user,'creator');
            $userService = new UserService();
            $creatorMap = $userService->getUserInfoByIds($creatorIdList);

            //step-2: 自动填充标签
            $sheetIdList = array_column($list,'id');
            $tagSheetService = new TagSheetService();
            $tagsMap = $tagSheetService->getTagsBySheetIds($sheetIdList);

            foreach ($user as $key=>$val) {
                $creator = $creatorMap[$val['creator']];
                $user[$key]['creator'] = $creator;
                $user[$key]['tags'] = $tagsMap[$val['id']];
                $user[$key]['status_name'] = SheetStatus::$map[$val['status']];
            }
        }
        if (!empty($admin)) {
            $creatorIdList = array_column($admin,'creator');
            $memberService = new MemberService();
            $creatorMap = $memberService->getMapByUids($creatorIdList);

            //step-2: 自动填充标签
            $sheetIdList = array_column($list,'id');
            $tagSheetService = new TagSheetService();
            $tagsMap = $tagSheetService->getTagsBySheetIds($sheetIdList);

            foreach ($admin as $key=>$val) {
                $creator = $creatorMap[$val['creator']];
                $admin[$key]['creator'] = $creator;
                $admin[$key]['tags'] = $tagsMap[$val['id']];
                $admin[$key]['status_name'] = SheetStatus::$map[$val['status']];
            }
        }

        if (empty($user)) {
            $list = $admin;
        } elseif (empty($admin)) {
            $list = $user;
        } else {
            $list = array_merge($admin, $user);
        }

        //对数组进行重新排序
        if ($sort == 2) {
            $list = my_sort($list, $order, SORT_DESC);
        } else {
            $list = my_sort($list, $order);
        }
        return $list;
    }
}